#include "memory/PagePoolSet.h"
#include "memory/PagePool.h"
namespace KCore
{
	void PagePoolSet::add(size_t sz,size_t num)
	{
		_pools.insert(std::make_pair(sz,new PagePool(sz,num)));
	}

	PagePool* PagePoolSet::getPool(size_t sz)
	{
		Pool::iterator it = _pools.find(sz);
		if(it!=_pools.end())
			return it->second;
		return 0;
	}

	PagePoolSet::~PagePoolSet()
	{
		Pool::iterator it = _pools.begin();
		for(;it!=_pools.end();++it)
		{
			delete it->second;
		}
		_pools.clear();
	}

	void* PagePoolSet::allocate(size_t sz,const char* file, int line, const char* func)
	{
		Pool::iterator it = _pools.begin();
		for(;it!=_pools.end();++it)
		{
			if(sz<=it->first)
				return (it->second)->allocate(sz,file,line,func);
		}
		return new char[sz];
	}

	void PagePoolSet::deallocate(void* p,size_t sz)
	{
		Pool::iterator it = _pools.begin();
		for(;it!=_pools.end();++it)
		{
			if(sz<=it->first)
			{
				(it->second)->deallocate((char*)p);
				return;
			}
		}
		return delete p;
	}
}

